import { _decorator, Component, Node,tween, Vec3 ,v3,Label} from 'cc';
import {GameMain} from "./GameMain";
import {NativeEvt} from "./GameConfig";
import Platform from "./Platform";
const { ccclass, property } = _decorator;

@ccclass('HpNotEnoughUI')
export class HpNotEnoughUI extends Component {

    public targetNode:Node=null;

    public cancelLabel:Label=null;

    public inited:boolean=false;

    onLoad()
    {
        this.init();
        Platform.createInsertAd();
    }

    public init()
    {
        if(this.inited)
            return;
        this.targetNode=this.node.getChildByName("window");
        this.cancelLabel=this.targetNode.getChildByName("cancelBtn").getChildByName("Label").getComponent(Label);
        this.inited=true;
    }

    start() {

    }

    public show()
    {
        if(this.node.active)
            return;
        this.init();

        if(GameMain.instance.startGameUI.node.active)
        {
            this.cancelLabel.string="取消";
        }
        else
        {
            this.cancelLabel.string="回到首页";
        }

        this.node.active=true;
        this.targetNode.setScale(0.3, 0.3,1);
        tween(this.targetNode)
            .to(0.3,{scale:Vec3.ONE}, { easing: 'backOut' })
            .start();
    }

    public close()
    {
        tween(this.targetNode).to(0.2,{scale:v3(0.5, 0.5, 0.5)}).call(()=>{
            this.node.active=false;
        }).start();
    }

    public onCancelClicked()
    {
        this.close();
        if(!GameMain.instance.startGameUI.node.active)
            GameMain.instance.backToStartUI();
    }

    public onResumeHpClicked()
    {
        GameMain.instance.setVedioAwardScene(NativeEvt.VedioResumeHp);
        Platform.playVideoAd(() =>
        {
            this.videoAward();
        });
    }

    public videoAward()
    {
        this.close();
        GameMain.instance.addHp(10);
        if(!GameMain.instance.startGameUI.node.active)
            GameMain.instance.prepareRetry();
    }
}


